/*
 * Copyright (C) 2017 Spreadtrum Communications Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "sprd_panel.h"
#include "sprd_dsi.h"
#include "dsi/mipi_dsi_api.h"
#include "sprd_dphy.h"

static uint8_t init_data[] = {
	0x23, 0x78, 0x00, 0x02, 0x11, 0x00,
	0x39, 0x00, 0x00, 0x06, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x10,
	0x39, 0x00, 0x00, 0x03, 0xC0, 0x63, 0x00,
	0x39, 0x00, 0x00, 0x03, 0xC1, 0x11, 0x02,
	0x39, 0x00, 0x00, 0x03, 0xC2, 0x11, 0x08,
	0x23, 0x00, 0x00, 0x02, 0xCC, 0x10,
	0x39, 0x00, 0x00, 0x11, 0xB0, 0x40, 0x03, 0x0B,0x13,0x19,0x0C,0x0D,0x0B,0x09,0x20,0x08,0x15,0x12,0x0E,0x1A,0x14,
	0x39, 0x00, 0x00, 0x11, 0xB1, 0x40,0x02,0xC9,0x10,0x15,0x0A,0x0A,0x09,0x09,0x24,0x08,0x15,0x11,0x1B,0x19,0x14,
	0x39, 0x00, 0x00, 0x06, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x11,
	0x23, 0x00, 0x00, 0x02, 0xB0, 0x4D,
	0x23, 0x00, 0x00, 0x02, 0xB1, 0x6F,
	0x23, 0x00, 0x00, 0x02, 0xB2, 0x07,
	0x23, 0x00, 0x00, 0x02, 0xB3, 0x80,
	0x23, 0x00, 0x00, 0x02, 0xB5, 0x47,
	0x23, 0x00, 0x00, 0x02, 0xB7, 0x85,
	0x23, 0x00, 0x00, 0x02, 0xB8, 0x21,
	0x23, 0x00, 0x00, 0x02, 0xB9, 0x10,
	0x23, 0x00, 0x00, 0x02, 0xC1, 0x78,
	0x23, 0x00, 0x00, 0x02, 0xC2, 0x78,
	0x23, 0x00, 0x00, 0x02, 0xD0, 0x88,
	0x39, 0x00, 0x00, 0x04, 0xE0, 0x00, 0xB4, 0x02,
	0x39, 0x00, 0x00, 0x0c, 0xE1, 0x08,0x00,0x0A,0x00,0x09,0x00,0x0B,0x00,0x00,0x44,0x44,
	0x39, 0x00, 0x00, 0x0e, 0xE2, 0x33,0x33,0x44,0x44,0x2D,0x00,0x2F,0x00,0x2E,0x00,0x30,0x00,0x00,
	0x39, 0x00, 0x00, 0x05, 0xE3, 0x00, 0x00, 0x33, 0x33,
	0x39, 0x00, 0x00, 0x03, 0xE4, 0x44, 0x44,
	0x39, 0x00, 0x00, 0x11, 0xE5, 0x0B,0x40,0xA0,0xA0,0x0D,0x40,0xA0,0xA0,0x0F,0x40,0xA0,0xA0,0x11,0x40,0xA0,0xA0,
	0x39, 0x00, 0x00, 0x05, 0xE6, 0x00, 0x00, 0x33, 0x33,
	0x39, 0x00, 0x00, 0x03, 0xE7, 0x44, 0x44,
	0x39, 0x00, 0x00, 0x11, 0xE8, 0x0C,0x40,0xA0,0xA0,0x0E,0x40,0xA0,0xA0,0x10,0x40,0xA0,0xA0,0x12,0x40,0xA0,0xA0,
	//0x39, 0x00, 0x00, 0x11, 0xEA, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00,
	0x39, 0x00, 0x00, 0x08, 0xEB, 0x02,0x00,0x4E,0x4E,0xEE,0x44,0x00,
	0x39, 0x00, 0x00, 0x03, 0xEC, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x11, 0xED, 0xFF,0xF1,0x04,0x56,0x72,0x3F,0xFF,0xFF,0xFF,0xFF,0xF3,0x27,0x65,0x40,0x1F,0xFF,
	//0x39, 0x00, 0x00, 0x0d, 0xEF, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
	0x39, 0x00, 0x00, 0x06, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
	0x23, 0x00, 0x00, 0x02, 0x35, 0x00,
	0x23, 0x0a, 0x00, 0x02, 0x29, 0x00,
	CMD_END
};

static int mipi_dsi_send_cmds(struct sprd_dsi *dsi, void *data)
{
	uint16_t len;
	struct dsi_cmd_desc *cmds = data;

	if ((cmds == NULL) || (dsi == NULL))
		return -1;

	for (; cmds->data_type != CMD_END;) {
		len = (cmds->wc_h << 8) | cmds->wc_l;
		mipi_dsi_gen_write(dsi, cmds->payload, len);
		if (cmds->wait)
			msleep(cmds->wait);
		cmds = (struct dsi_cmd_desc *)(cmds->payload + len);
	}
	return 0;
}

static int st7701_init(void)
{
	struct sprd_dsi *dsi = &dsi_device;
	struct sprd_dphy *dphy = &dphy_device;

	mipi_dsi_lp_cmd_enable(dsi, true);
	mipi_dsi_send_cmds(dsi, init_data);
	mipi_dsi_set_work_mode(dsi, SPRD_MIPI_MODE_VIDEO);
	mipi_dsi_state_reset(dsi);
	mipi_dphy_hs_clk_en(dphy, true);

	return 0;
}

static int st7701_readid(void)
{
	struct sprd_dsi *dsi = &dsi_device;
	uint8_t read_buf[4] = {0};
	int value = 0 ;

	mdelay(150);
	sprd_gpio_request(NULL, CONFIG_LCM_GPIO_ID);
	sprd_gpio_direction_input(NULL, CONFIG_LCM_GPIO_ID);
	value = sprd_gpio_get(NULL,CONFIG_LCM_GPIO_ID);

	if(value > 0x00) {
		pr_err("st7701 coe match value failed!\n");
		return -1;
	}

	mipi_dsi_lp_cmd_enable(dsi, true);
	mipi_dsi_set_max_return_size(dsi, 2);
	mipi_dsi_dcs_read(dsi, 0xa1, &read_buf[0], 2);

	if ((read_buf[0] == 0x88) && (read_buf[1] == 0x02)) {
		pr_info("st7701 coe read id success!\n");
		return 0;
	}

	pr_err("st7701 coe read id failed!\n");
	return -1;
}

static int st7701_power(int on)
{
	if (on) {
		sprd_gpio_request(NULL, CONFIG_LCM_GPIO_RSTN);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 1);
		mdelay(50);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 0);
		mdelay(50);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 1);
		mdelay(150);
	} else {
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 0);
		mdelay(5);
	}

	return 0;
}

static struct panel_ops st7701_ops = {
	.init = st7701_init,
	.read_id = st7701_readid,
	.power = st7701_power,
};

static struct panel_info st7701_info = {
	/* common parameters */
	.lcd_name = "lcd_st7701_coe_mipi_wvga",
	.type = SPRD_PANEL_TYPE_MIPI,
	.bpp = 24,
//	.fps = 3,
	.width = 480,
	.height = 800,

	/* DPI specific parameters */
	.pixel_clk = 32000000,
	.rgb_timing = {
		.hfp = 80,
		.hbp = 70,
		.hsync = 10,
		.vfp = 18,
		.vbp = 20,
		.vsync = 10,
	},

	/* MIPI DSI specific parameters */
	.phy_freq = 500000,
	.lane_num = 2,
	.work_mode = SPRD_MIPI_MODE_VIDEO,
	.burst_mode = PANEL_VIDEO_BURST_MODE,
	.nc_clk_en = false,
};

struct panel_driver st7701_coe_driver = {
	.info = &st7701_info,
	.ops = &st7701_ops,
};
